package com.freak.freakmusic.model.homepage

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.flyco.tablayout.listener.OnTabSelectListener
import com.freak.freakmusic.R
import com.freak.freakmusic.base.BaseAbstractMvpFragment
import com.freak.freakmusic.model.base.adapter.MainTabAdapter
import com.freak.freakmusic.model.homepage.classify.ClassifyActivity
import com.freak.freakmusic.model.homepage.tab.TabFragment
import com.freak.freakmusic.utils.ToastUtil
import com.freak.freakmusic.view.load.LoadingViewDialog
import com.freak.kotlinhttpmanager.kotlinhttpmanager.log.LogUtil
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.header.ClassicsHeader
import com.scwang.smartrefresh.layout.listener.OnRefreshListener
import com.ximalaya.ting.android.opensdk.constants.DTransferConstants
import com.ximalaya.ting.android.opensdk.model.category.CategoryList
import kotlinx.android.synthetic.main.fragment_homepage.*
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.HashMap


class HomepageFragment :
    BaseAbstractMvpFragment<HomepageContract.View, HomepageContract.Presenter>(),
    HomepageContract.View, View.OnClickListener, OnTabSelectListener, OnRefreshListener,
    ViewPager.OnPageChangeListener {
    private lateinit var mClassicsHeader: ClassicsHeader
    private var isScroll: Boolean = false
    override fun onPageScrollStateChanged(state: Int) {
        when (state) {
            ViewPager.SCROLL_STATE_DRAGGING -> {
                isScroll = false
                LogUtil.e("SCROLL_STATE_DRAGGING")
            }
            ViewPager.SCROLL_STATE_IDLE -> {
                LogUtil.e("滑动结束")
                if (view_pager_homepage.currentItem == view_pager_homepage.adapter!!.count - 1 && !isScroll) {
                    LogUtil.e("滑动到最后一页")
                    gotoActivity(ClassifyActivity::class.java)
                }
            }
            ViewPager.SCROLL_STATE_SETTLING -> {
                LogUtil.e("SCROLL_STATE_SETTLING")
                isScroll = true
            }
        }

    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        val tabFragment =
            homepageTabAdapter.mFragments[view_pager_homepage.currentItem] as TabFragment
        tabFragment.onRefresh(refreshLayout)

    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
    }

    override fun onPageSelected(position: Int) {
    }

    override fun onTabSelect(position: Int) {
        LogUtil.e("选择$position")
    }

    override fun onTabReselect(position: Int) {
        LogUtil.e("重复选择$position")
    }

    private lateinit var titleList: MutableList<String>
    private lateinit var fragmentList: MutableList<Fragment>
    private lateinit var homepageTabAdapter: MainTabAdapter

    override fun getCategoriesSuccess(categoryList: CategoryList) {
        LogUtil.e("回调成功")
        LoadingViewDialog.instance.dismiss()
        titleList.clear()
        fragmentList.clear()
        titleList.add("全部")
        val bundle = Bundle()
        bundle.putString("type", "全部")
        val allTabFragment = TabFragment()
        allTabFragment.arguments = bundle
        titleList.add("推荐")
        val recommendBundle = Bundle()
        recommendBundle.putString("type", "推荐")
        val recommendTabFragment = TabFragment()
        recommendTabFragment.arguments = recommendBundle
        fragmentList.add(allTabFragment)
        fragmentList.add(recommendTabFragment)
        for (categoryItem in categoryList.categories) {
            titleList.add(categoryItem.categoryName)
            val bundle = Bundle()
            bundle.putString("type", categoryItem.categoryName)
            val tabFragment = TabFragment()
            tabFragment.arguments = bundle
            fragmentList.add(tabFragment)
        }
        homepageTabAdapter = MainTabAdapter(
            childFragmentManager,
            fragmentList as ArrayList<Fragment>, titleList as ArrayList<String>
        )
        view_pager_homepage.adapter = homepageTabAdapter
        view_pager_homepage.setScanScroll(true)
        view_pager_homepage.addOnPageChangeListener(this)
        sliding_tab_layout.setViewPager(
            view_pager_homepage, titleList.toTypedArray(), mActivity,
            fragmentList as ArrayList<Fragment>
        )
        view_pager_homepage.currentItem = 0
    }

    override fun onClick(v: View?) {
        when (v!!.id) {
            //分类
            R.id.constraint_layout_homepage_classify -> {
                gotoActivity(ClassifyActivity::class.java)
                LogUtil.e("点击")
                val map = HashMap<String, String>()
//                mPresenter?.getCategories(map)
                mPresenter?.getColdbootGenres(map)
                mPresenter?.getColdbootSubGenres(map)
                mPresenter?.getColdBootDetail(map)
                mPresenter?.getAnnouncerCategoryList(map)

                val map1 = HashMap<String, String>()
                map1[DTransferConstants.COLDBOOT_GENRE] = "美女"
                map1[DTransferConstants.COLDBOOT_SUB_GENRE] = "学生党"
                LogUtil.d("" + map1)
                mPresenter?.getColdBootTag(map1)
            }
        }
    }

    override fun testSuccess() {
        LogUtil.e("成功")
        showToast("成功")
    }


    override val layoutId: Int
        get() = R.layout.fragment_homepage

    override fun createPresenter(): HomepagePresenter {
        return HomepagePresenter()
    }

    override fun initEventAndData(view: View) {
        titleList = arrayListOf()
        fragmentList = arrayListOf()
        mClassicsHeader = refreshLayoutHomepage.refreshHeader as ClassicsHeader
        mClassicsHeader.setLastUpdateTime(Date(System.currentTimeMillis()))
        mClassicsHeader.setTimeFormat(SimpleDateFormat("更新于 MM-dd HH:mm", Locale.CHINA))
        refreshLayoutHomepage.setOnRefreshListener(this)
        initOnClick()
    }

    private fun initOnClick() {
        constraint_layout_homepage_classify.setOnClickListener(this)

    }

    override fun initLazyData() {
        val map = HashMap<String, String>()
        mPresenter?.getCategories(map)
        LoadingViewDialog.instance.show(mActivity)
    }

    override fun showLoading() {
    }

    override fun showToast(toast: String) {
        ToastUtil.shortShow(toast)
    }
}